﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.Services;

namespace UCWebpart
{
    /// <summary>
    /// SetState 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class SetState : System.Web.Services.WebService
    {
        /// <summary>
        /// 回传审批流结果
        /// </summary>
        /// <param name="guid">审批流的guid</param>
        /// <param name="status">最后的审批结果（通过审批或是被拒绝）</param>
        /// <param name="reason">若审批被拒绝，拒绝的原因</param>
        [WebMethod]
        public void updateVenueStatueByApprove(string guid, string status, string reason)
        {
            string conn = WebConfigurationManager.ConnectionStrings["meetingroomManagementConnectionString"].ConnectionString;// "Data Source=mesdbsrv.gwkf.cn;Initial Catalog=VenueBooking;User ID=vbuser;Password=vb_2013";
            string insertLog = "insert into [VenueBooking].[dbo].[MTR_Approve] values('" + guid + "','" + status + "','" + reason + "','" + DateTime.Now + "')";
            WFStatus ws = new WFStatus();
            ws.id = guid;
            ws.status = status;
            ws.reason = reason;
            string sqlquery = string.Format(@" select * from [VenueBooking].[dbo].[MTR_bas_BookingManage] bm,[VenueBooking].[dbo].MTR_bas_BookingSchedul bs,[VenueBooking].[dbo].MTR_bas_Meetingroom m 
                where bs.planId = bm.bookingID and bm.Meetingroom = m.mtrId and bm.undefine3='{0}'", ws.id);
            try
            {
                Worker.doSql(insertLog, conn);//日志
                DataTable dt = Worker.GetDataSet(sqlquery, conn).Tables[0];
                if (dt.Rows.Count != 1)
                {
                    return;
                }
                string meetingTheme = dt.Rows[0]["meetingTheme"].ToString();
                string userName = dt.Rows[0]["bookingPerson"].ToString();
                //通过审批
                if (ws.status=="true")
                {
                    string startDate = dt.Rows[0]["meetingStartDate"].ToString().Split(' ')[0];
                    string st = dt.Rows[0]["startTime"].ToString();
                    string endDate = dt.Rows[0]["meetingEndDate"].ToString().Split(' ')[0];
                    string et = dt.Rows[0]["endTime"].ToString();
                    DateTime startTime = DateTime.Parse(startDate + " " + st);
                    DateTime endTime = DateTime.Parse(endDate + " " + et);
                    string address = dt.Rows[0]["mtrAddress"].ToString() + dt.Rows[0]["mtrAddress1"].ToString() + " " + dt.Rows[0]["mtrName"].ToString();

                    string meetingAbstract = dt.Rows[0]["meetingAbstract"].ToString();
                    string participants = dt.Rows[0]["participants"].ToString();
                    string ccs = dt.Rows[0]["undefine31"].ToString();
                    string[] attendees = participants.Split(';');
                    string[] cc = ccs.Split(';');
                    string keyId = dt.Rows[0]["bookingID"].ToString();

                    Worker w = new Worker();
                    w.SendMeetingRequest(startTime, endTime, address, meetingTheme, meetingAbstract, attendees,cc,
                        keyId, false, userName, userName + "@kaifa.cn", false, "");

                    string strSql = string.Format(@"update [VenueBooking].[dbo].[MTR_bas_BookingManage] set bookingType = '{0}' ,bookingState = 2 where undefine3 = '{1}'","通过审批", ws.id);
                    Worker.doSql(strSql, conn);
                }
                //审批被拒
                else if (ws.status=="false")
                {
                    string mailSub = "审批拒绝提醒";
                    string mailBody = "您的主题为" + meetingTheme + "的活动未通过审批,原因如下：" + ws.reason;
                    Worker.sendEmailMessage(userName + "@kaifa.cn", mailSub, "venueAdmin@kaifa.cn", mailBody);
                    string strSql = @"update [VenueBooking].[dbo].[MTR_bas_BookingManage]
                        set bookingType = '未通过审批' ,bookingState = 5 where undefine3 = '" + ws.id + "'";
                    Worker.doSql(strSql, conn);
                }
            }
            catch (Exception ee)
            {
                string insertLog1 = "insert into [VenueBooking].[dbo].[MTR_Approve]  values('" + guid + "','" + 0 + "','" + ee.Message + "','" + DateTime.Now + "')";
                Worker.doSql(insertLog1, conn);//日志
            }
        }

        public class WFStatus
        {
            public string id { get; set; }
            public string status { get; set; }
            public string reason { get; set; }
        }
        
    }
}
